﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace DBCExtractor
{
    public partial class MainWindow : Form
    {
        private DirectoryInfo info;
        private int done = 0;
        private int numFiles = 0;

        public MainWindow()
        {
            InitializeComponent();
        }

        private bool hasFile(string name)
        {
            DirectoryInfo di = new DirectoryInfo(Directory.GetCurrentDirectory() + "\\xml");

            foreach (FileInfo fi in di.GetFiles())
            {
                if (fi.Name == name)
                    return true;
            }

            return false;
        }

        private void btn_directory_Click(object sender, EventArgs e)
        {
            if (this.folderBrowser.ShowDialog() == DialogResult.OK)
            {
                this.tb_directory.Text = this.folderBrowser.SelectedPath;

                info = new DirectoryInfo(this.tb_directory.Text);
                foreach (FileInfo finfo in this.info.GetFiles())
                {
                    int length = finfo.Name.Length;
                    if (finfo.Name[length - 1] != 'c' ||
                        finfo.Name[length - 2] != 'b' ||
                        finfo.Name[length - 3] != 'd')
                        continue;

                    numFiles++;
                }

                this.lbl_progress.Text = "Progress: 0/" + numFiles;
                this.progressBar.Maximum = numFiles;
            }
        }

        private void btn_start_Click(object sender, EventArgs e)
        {
            if (this.tb_directory.Text == String.Empty)
            {
                MessageBox.Show("You have to choose a directory first!");
            }
            else
            {
                done = 0;
                foreach (FileInfo finfo in this.info.GetFiles())
                {
                    int length = finfo.Name.Length;
                    if(finfo.Name[length-1] != 'c' ||
                        finfo.Name[length-2] != 'b' ||
                        finfo.Name[length-3] != 'd')
                        continue;

                    string str = finfo.Name.Substring(0, length - 4);

                    if (!hasFile(str + ".xml"))
                    {
                        MessageBox.Show("No .xml file found for \""+str+"\"!");
                        this.progressBar.PerformStep();
                        continue;
                    }
                        

                    DBC dbc = new DBC(finfo.FullName, "sql\\"+str+".sql", "xml\\"+str+".xml");
                    dbc.ConvertFile();

                    this.progressBar.PerformStep();
                    ++done;

                    this.lbl_progress.Text = "Progress: " + done + " (" + str + ")/" + numFiles;
                }
            }
        }
    }
}
